#!/bin/sh

target_detector_module_name="@target_detector_module_name@"
target_detector_module="${target_detector_module_name}.ko"
test_module_name="@test_module_name@"
test_module="test_module/${test_module_name}.ko"

target_is_loaded_file="/sys/module/${target_detector_module_name}/parameters/target_is_loaded"

if ! @INSMOD@ "${target_detector_module}" "target_name=${test_module_name}"; then
    printf "Failed to insert target detector module.\n"
    exit 1
fi

target_is_loaded=`cat "${target_is_loaded_file}"`

if test "${target_is_loaded}" != "0"; then
    printf "Target is not loaded, but indicator say that it is.\n"
    @RMMOD@ "${target_detector_module_name}"
    exit 1
fi

if ! @INSMOD@ "${test_module}"; then
    printf "Failed to insert test module.\n"
    @RMMOD@ "${target_detector_module_name}"
    exit 1
fi

target_is_loaded=`cat "${target_is_loaded_file}"`

if test "${target_is_loaded}" = "0"; then
    printf "Target is loaded, but indicator say that it is not.\n"
    @RMMOD@ "${test_module}"
    @RMMOD@ "${target_detector_module}"
    exit 1
fi


if ! @RMMOD@ "${test_module}"; then
    printf "Failed to unload test module.\n"
    exit 1
fi

target_is_loaded=`cat "${target_is_loaded_file}"`

if ! @RMMOD@ "${target_detector_module}"; then
    printf "Failed to unload target detector module.\n"
    exit 1
fi

if test "${target_is_loaded}" != "0"; then
    printf "Target has unloaded, but indicator say that it has not.\n"
    exit 1
fi

